'use strict';
const { cloneDeep, last, get } = require('lodash');

class RecomeData {
  constructor(ctx) {
    const params = this._assembleAllParams(ctx);
    this.parsed = cloneDeep(params);
  }

  _assembleAllParams(ctx) {
    return {
      body: ctx.request.body,
      query: ctx.request.query,
      path: ctx.params,
      header: ctx.request.header,
    };
  }

  get(path, parsed = true) {
    if (parsed) {
      const value = get(this.parsed, path, null);
      if (value == null) {
        const keys = path.split('.');
        const key = last(keys);
        return get(this.parsed.default, key);
      }
      return value;
    }
    return get(this.data, path);
  }
}

module.exports = RecomeData;
